/**
* @Author: 李晨光
* @Date: 2019/12/4
* @Version: 1.0
* @Last Modified by: 李晨光
* @Last Modified time: 2019/12/4
**/
<comment>
  组织选择
  # 使用方法
  <Org ref="org" v-model="org" @change="orgChange"></Org>
  methods: {
  打开
  openOrgDialog() {
  this.$refs.org.openOrgDialog()
  },
  关闭
  closeOrgDialog() {
  this.$refs.org.closeOrgDialog()
  },
  值变化
  orgChange(ev) {
  console.log(ev)
  }
  },
  v-model [{label: '1',
  value: '1',
  disabled: false,
  checked: false,
  hasChecked: true,
  hasChildren: true
  }]

</comment>
<template>
  <div id="Org">
    <Dialog ref="orgPop" :title="title" :open="open" @close="closeOrgDialog">
      <Select
        :lists="lists"
        :orgType="orgType"
        @confirm="confirm"
        v-model="checked"
        @expand="expand"
        @unexpand="unexpand"
        slot="dialog-content"
      ></Select>
    </Dialog>
  </div>
</template>

<script>
  import Dialog from "@/dialog/Dialog.vue";
  import Select from "@/select/org/OrgTreeThrough.vue";

  export default {
    name: "Org",
    components: {
      Select,
      Dialog
    },
    props: {
      // orgType  类型
      // all 上级 + 本级 + 下级 基础组织 +  项目(组织、项目可选)
      // allOrg 上级 + 本级 + 下级 基础组织(组织可选)
      // allOrgProject 上级 + 本级 + 下级 基础组织 + 项目  (项目可选)
      // currOrg 本级 + 下级 基础组织(组织可选)
      // currOrgProject 本级 + 下级 基础组织 + 项目(项目可选)
      // allContacts 上级 + 本级 + 下级 基础组织+ 项目 + 参建单位（参建单位可选）
      // currContacts 本级 + 下级 基础组织 + 项目 + 参建单位（参建单位可选）
      orgType: {
        type: String,
        default: "all"
      },
      title: {
        type: String,
        default: "选择"
      },
      // 选择结果
      checked: {
        type: Array,
        default: () => {
          return [];
        }
      },
      open: {
        type: Boolean,
        default: false
      },
      customRoot: {
        type: Array,
        default: () => {
          return [];
        }
      }
    },
    model: {
      prop: "checked",
      event: "change"
    },
    data() {
      return {
        api: {
          getOrgTreeDataApi: "/safety/getOrgs", // 获取基础组织
          getContactsDataApi: "/safety/corp/app", // 获取参建
          getProjectsDataApi: '/safety/project/allProject', // 获取项目
        },
        // 组织信息列表
        lists: [],
        pinfo: {
          pid: "",
          pname: "",
          type: ""
        }
      };
    },
    computed: {},
    created() {
      // type  0  基础组织  1  项目  2 参建单位
      // 切换上下文组织、项目 与 业务中选择组织、项目
      this.setPList();
    },
    mounted() {
    },
    watch: {},
    methods: {
      // 打开
      openOrgDialog() {
        this.$emit("open", true);
      },
      // 关闭
      closeOrgDialog() {
        this.$emit("close", true);
      },
      // 确认回调
      confirm(ev) {
        this.$emit("change", ev);
        this.$emit("confirm", true);
      },
      async getFistList (id) {
          let _this = this;
          let obj = {
              "pageNo":-1,
              "pageSize":0
          }
          obj['obj'] = {
              "organizationAbbreviation": "",
              "organizationCode": "",
              "organizationName": '',
              "organizationStatus": "",
              "organizationType": "",
              "id": id,
              "tenantId": ""
          }
          this.$A.Go('post','/safety/getChildOrgs',obj).then(response=>{
              // if(success.code == 200){
              //   let data = success.body || [];
              //   return data
                  
              // }else{
              //   return [];
              // }
              Util.processRes(response, () => {
                this.lists = [...this.lists, ...this.dealData(response.body[0] && response.body[0]['projects'] || [], "id", "projectName")]
              });
              Util.processError(this, response);
          },function(err){
              return [];
          })
      },
      setPList() {

        console.log(this.customRoot)
        if (this.customRoot.length) {
          this.lists = this.dealData(this.customRoot, 'value', 'label')
          return
        }
        let userInfo = this.$A.GS("userInfo");
        let orgInfo = this.$A.GS("orgInfo");
        console.log(userInfo)
        console.log(orgInfo)
        let disabled = false;
        if (
          this.orgType == "allOrg" ||
          this.orgType == "allOrgProject" ||
          this.orgType == "allContacts" ||
          this.orgType == "all"
        ) {
          this.pinfo.pid = userInfo["orgId"];
          this.pinfo.pname = userInfo["orgName"];
          this.pinfo.type = userInfo["type"];
          if (this.orgType == "allOrgProject" || this.orgType == "allContacts") {
            disabled = this.pinfo.type == "0" ? true : false;
          } else {
            disabled = false;
          }
        } else {
          this.pinfo.pid = orgInfo["orgId"];
          this.pinfo.pname = orgInfo["orgName"];
          this.pinfo.type = orgInfo["type"];
          if (
            this.orgType == "currOrgProject" ||
            this.orgType == "currContacts"
          ) {
            disabled = this.pinfo.type == "0" ? true : false;
          } else {
            disabled = false;
          }
        }
        if (this.pinfo.pid) {
          this.lists = this.dealData([
            {
              label: this.pinfo.pname,
              value: this.pinfo.pid,
              type: this.pinfo.type,
              reGetProject: 1
            }
          ],'value', 'label');
        } else {
          this.lists = [];
        }
      },
      // 展开获取下级
      expand(item) {
        if (item.type == "1") {
          this.getContactsData(item);
        } else {
          this.getOrgData(item);
        }
      },
      unexpand(item) {
        if (item.value == "org") {
          this.setPList();
        } else {
          this.getOrgData(item);
        }
      },
      // 获取组织信息
      getOrgData(item) {
        this.$A
          .Go("post", this.api.getOrgTreeDataApi, {
            obj: {
              organizationAbbreviation: "",
              organizationCode: "",
              organizationStatus: "",
              organizationType: "",
              parentOrganization: item.value,
              tenantId: ""
            },
            pageNo: -1,
            pageSize: 10
          })
          .then(response => {
            Util.processRes(response, () => {
              if (
                this.orgType == "allOrgProject" ||
                this.orgType == "currOrgProject" ||
                this.orgType == "currContacts" ||
                this.orgType == "allContacts" ||
                this.orgType == "all"
              ) {
                // let projects = this.getProjects(response.body);
                // console.log(projects)
                this.lists = [
                  ...this.dealData(
                    response.body,
                    "id",
                    "organizationName"
                  ),
                  ...(item.projects ? this.dealData(item.projects, "id", "projectName") : [])
                ];
                if (item.reGetProject) {
                  this.getFistList(item.value)
                }

              } else {
                this.lists = this.dealData(
                  response.body,
                  "id",
                  "organizationName"
                );
              }
            });
            Util.processError(this, response);
          })
          .catch(err => {
            console.log(err);
          });
      },
      // 获取组织信息
      getContactsData(item) {
        this.$A
          .Go("post", this.api.getContactsDataApi, {
            obj: {
              projectId: item.value
            },
            pageNo: -1,
            pageSize: 10
          })
          .then(response => {
            Util.processRes(response, () => {
              this.lists = this.dealData(response.body, "id", "name");
            });
            Util.processError(this, response);
          })
          .catch(err => {
            console.log(err);
          });
      },
      // 获取项目
      getProjectsData(item) {
        this.$A
          .Go("post", this.api.getProjectsDataApi, {
            obj: {
              // orgId: item.value
              parentId: item.value
            },
            pageNo: -1,
            pageSize: 10
          })
          .then(response => {
            Util.processRes(response, () => {
              this.lists = [...this.lists, ...this.dealData(response.body, "id", "projectName")]
            });
            Util.processError(this, response);
          })
          .catch(err => {
            console.log(err);
          });
      },
      dealData(data, value = "", label = "") {
        let arr = [];
        data.length &&
        data.forEach(item => {
          let obj = {};
          obj = item;
          obj.value = item[value];
          obj.label = item[label];
          obj.type = item["type"];
          if (
            this.orgType == "allOrgProject" ||
            this.orgType == "currOrgProject" ||
            this.orgType == "currContacts" ||
            this.orgType == "allContacts" ||
            this.orgType == "all"

          ) {
            if (
              this.orgType == "allOrgProject" ||
              this.orgType == "currOrgProject" ||
              this.orgType == "all"
            ) {
              if (this.orgType == "all") {
                obj.disabled = false
                obj.hasChildren = obj.type == "0" ? true : false;
              } else {
                obj.disabled = obj.type == "0" ? true : false;
                obj.hasChildren = obj.type == "0" ? true : false;
              }
            } else if (
              this.orgType == "currContacts" ||
              this.orgType == "allContacts"
            ) {
              obj.disabled = obj.type == "0" || obj.type == "1" ? true : false;
              obj.hasChildren =
                obj.type == "0" || obj.type == "1" ? true : false;
            }
            obj.hasChecked = !obj.disabled;
          } else {
            obj.hasChildren = true;
            obj.hasChecked = true;
            obj.disabled = false;
          }
          arr.push(obj);
        });
        return arr;
      },
      getProjects(data) {
        let arr = [];
        data.length &&
        data.forEach(item => {
          arr = [...arr, ...item.projects];
        });
        return arr;
      }
    },
    destroyed() {
    }
  };
</script>

<style lang="scss">
</style>
